package com.papers.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.papers.common.annotation.Log;
import com.papers.common.annotation.RepeatSubmit;
import com.papers.common.core.controller.BaseController;
import com.papers.common.core.domain.PageQuery;
import com.papers.common.core.domain.R;
import com.papers.common.core.page.TableDataInfo;
import com.papers.common.core.validate.AddGroup;
import com.papers.common.core.validate.EditGroup;
import com.papers.common.enums.BusinessType;
import com.papers.common.excel.ExcelResult;
import com.papers.common.utils.poi.ExcelUtil;
import com.papers.system.domain.TJuanDepartment;
import com.papers.system.domain.bo.TJuanDepartmentBo;
import com.papers.system.domain.vo.TJuanDepartmentVo;
import com.papers.system.service.ITJuanDepartmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
 * 院系
 *
 * @author papers
 * @date 2022-10-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/juanDepartment")
public class TJuanDepartmentController extends BaseController {

    private final ITJuanDepartmentService iTJuanDepartmentService;

    /**
     * 查询院系列表
     */
    @SaCheckPermission("system:juanDepartment:list")
    @GetMapping("/list")
    public TableDataInfo<TJuanDepartmentVo> list(TJuanDepartmentBo bo, PageQuery pageQuery) {
        return iTJuanDepartmentService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询所有院系
     *
     * @return
     */
    @GetMapping("/getDepartmentList")
    public R getDepartmentList() {
        return iTJuanDepartmentService.getDepartmentList();
    }

    /**
     * 导入院系列表
     *
     * @return
     */
    @SaCheckPermission("system:juanDepartment:import")
    @Log(title = "院系", businessType = BusinessType.IMPORT)
    @PostMapping("/importFile")
    public R importFile(@RequestPart("file") MultipartFile file) {
        InputStream fis = null;
        try {
            fis = file.getInputStream();
            ExcelResult<TJuanDepartmentVo> result = ExcelUtil.importExcel(fis, TJuanDepartmentVo.class, true);
            List<TJuanDepartmentVo> voList = result.getList();
            List<TJuanDepartment> list = BeanUtil.copyToList(voList, TJuanDepartment.class);
            if (list.size() == 0) {
                return R.fail("请先添加数据");
            }
            return iTJuanDepartmentService.save(list);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 导出院系列表
     */
    @SaCheckPermission("system:juanDepartment:export")
    @Log(title = "院系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TJuanDepartmentBo bo, HttpServletResponse response) {
        List<TJuanDepartmentVo> list = iTJuanDepartmentService.queryList(bo);
        ExcelUtil.exportExcel(list, "院系", TJuanDepartmentVo.class, response);
    }

    /**
     * 获取院系详细信息
     *
     * @param departId 主键
     */
    @SaCheckPermission("system:juanDepartment:query")
    @GetMapping("/{departId}")
    public R<TJuanDepartmentVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable String departId) {
        return R.ok(iTJuanDepartmentService.queryById(departId));
    }

    /**
     * 新增院系
     */
    @SaCheckPermission("system:juanDepartment:add")
    @Log(title = "院系", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TJuanDepartmentBo bo) {
        //int i = iTJuanDepartmentService.DepartCodeUnique(bo);
        //if (i == 0) {
        //    return R.fail("新增院系失败，院系编号" + bo.getDepartCode() + "已存在");
        //}
        return toAjax(iTJuanDepartmentService.insertByBo(bo) ? 1 : 0);

    }

    /**
     * 修改院系
     */
    @SaCheckPermission("system:juanDepartment:edit")
    @Log(title = "院系", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TJuanDepartmentBo bo) {
        //int i = iTJuanDepartmentService.DepartCodeUnique(bo);
        //if (i == 0) {
        //    return R.fail("修改院系失败，院系编号" + bo.getDepartCode() + "已存在");
        //}
        return toAjax(iTJuanDepartmentService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除院系
     *
     * @param departIds 主键串
     */
    @SaCheckPermission("system:juanDepartment:remove")
    @Log(title = "院系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{departIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] departIds) {
        return toAjax(iTJuanDepartmentService.deleteWithValidByIds(Arrays.asList(departIds), true) ? 1 : 0);
    }

    /**
     * 查询专业院系名称
     *
     * @return
     */
    @GetMapping("/getMajorDepNameList")
    public R<List<TJuanDepartmentVo>> getDeptNameList() {
        return R.ok(iTJuanDepartmentService.getMajorDepName());
    }

}
